package ru.averta.notes.service.html;

import de.l3s.boilerpipe.BoilerpipeProcessingException;
import de.l3s.boilerpipe.extractors.ArticleExtractor;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * <code>HtmlContentExtractorImpl</code>
 *
 * @author fedor.belov (fedor.belov@mail.ru)
 *         Date: 17.09.11 0:30
 */

@Service
public class HtmlContentExtractorImpl implements HtmlContentExtractor {

    private static final Logger logger = LoggerFactory.getLogger(HtmlContentExtractorImpl.class);

    @Override
    public String getContent(URL url) {
        if (url == null) return "";

        String answer;

        /*answer = getBoilerpipeContent(url);
        if (!isEmpty(answer)) return answer;

        logFail(url, "Boilerpipe");*/

        answer = getJSoupContent(url);
        if (!isEmpty(answer)) return answer;

        logFail(url, "JSpoup");

        answer = getPlainHTML(url);
        if (!isEmpty(answer)) return answer;

        logFail(url, "PlainHTML");
        return "";
    }

    private boolean isEmpty(String answer) {
        return (answer == null || answer.equals(""));
    }

    /**
     * Получаем контент, используя метод http://code.google.com/p/boilerpipe/
     *
     * @param url
     * @return
     */
    private String getBoilerpipeContent(URL url) {
        try {
            return ArticleExtractor.getInstance().getText(url);
        } catch (BoilerpipeProcessingException e) {
            logException(url, e);
            return null;
        }
    }

    private void logFail(URL url, String method) {
        logger.debug("Не удалось получить контент {}, используя метод {}", url.toString(), method);
    }

    private void logException(URL url, Throwable e) {
        logger.info("Ошибка при обработке URL'a " + url.toString(), e);
    }

    /**
     * Получаем контен, используя http://jsoup.org/download
     *
     * @param url
     * @return
     */
    private String getJSoupContent(URL url) {
        try {
            return Jsoup.parse(url, 5000).text();
        } catch (IOException e) {
            logException(url, e);
            return null;
        }
    }

    /**
     * Тупо читаем HTML
     *
     * @param url
     * @return
     */
    private String getPlainHTML(URL url) {
        try {
            InputStream is = (InputStream) url.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            logException(url, e);
            return null;
        }
    }
}
